import json
from domian.FileReader import FileReader
from pyecharts.charts import Bar, Timeline
from pyecharts.options import *

print("设计封装")
data_list = []
sales_by_date = {}
date_list = []
sale_list = []
with open("2011年2月销售数据JSON.txt", "r", encoding="utf-8") as f:
    data = f.readlines()
    for e in data:
        dict = json.loads(e)
        sale_data = FileReader(date=dict["date"], order_id=dict["order_id"], sales=dict["money"],
                               province=dict["province"])
        data_list.append(sale_data)
for data in data_list:
    list2 = []
    if data.date not in list2:
        list2.append(data.date)
    try:
        sales_by_date[data.date] += data.sales
    except Exception as e:
        sales_by_date[data.date] = 0
        sales_by_date[data.date] += data.sales
for e in sales_by_date:
    date_list.append(e)
    sale_list.append(sales_by_date[e])



data_list2 = []
sales_by_date2={}
with open("2011年1月销售数据.txt", "r", encoding="utf-8") as f:
    data2 = f.readlines()
    for e in data2:
        list3 = e.split(",")
        sale_data2 = FileReader(list3[0], list3[1], int(list3[2]), list3[3].strip())
        data_list2.append(sale_data2)
for data in data_list2:
    list3 = []
    if data.date not in list3:
        list3.append(data.date)
    try:
        sales_by_date2[data.date] += data.sales
    except Exception as e:
        sales_by_date2[data.date] = 0
        sales_by_date2[data.date] += data.sales
list5=[]
list6=[]
for e in sales_by_date2:
    list5.append(e)
    list6.append(sales_by_date2[e])
list5.extend(date_list)
list6.extend(sale_list)
bar = Bar()
bar.add_xaxis(list5)
bar.add_yaxis("销售额", list6, label_opts=LabelOpts(is_show=False))
bar.set_global_opts(
    xaxis_opts=AxisOpts(name="时间/号", axislabel_opts=LabelOpts(rotate=-90, interval=1, font_size=10)),
)
bar.render("销售额.html")